package com.commute.android;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.commute.CommuteEvent;
import com.commute.data.Route;
import com.commute.data.Stop;
import com.commute.data.StopTime;
import com.commute.data.Trip;
import com.commute.planner.CommutePlanner;
import com.commute.planner.RecommendedTrip;
import com.commute.schedule.Parser;

public class MainActivity extends Activity {
	
	public static CommuteEvent comeve;
	public static CommutePlanner complan;
	public static double lon = 0.0;
	public static double lat = 0.0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		//BEGIN HARDCODED DEMO DATA
		
		LinkedList<RecommendedTrip> recTrips = new LinkedList<RecommendedTrip>();
		
		RecommendedTrip rt1 = new RecommendedTrip();
		RecommendedTrip rt2 = new RecommendedTrip();
		RecommendedTrip rt3 = new RecommendedTrip();
		Stop s1 = new Stop("Porsö centrum", 21, 1.1, 1.2);
		Stop s2 = new Stop("Smedjegatan", 11, 2.1, 2.2);
		Stop s3 = new Stop("Hertsön", 31, 3.1, 3.2);
		Route r1 = new Route("FEMMAN",5);
		Route r2 = new Route("ETTAN",1);
		Route r3 = new Route("FYRAN",4);
		Route r4 = new Route("ÅTTAN",8);
		Date d1 = new Date();
		Date d2 = new Date();
		Trip loltrip = new Trip(1,1, r1);
		d2.setHours(16);
		StopTime st1 = new StopTime(1, loltrip, 2, s1, 3, d1, d2 );
		StopTime st2 = new StopTime(1, loltrip, 2, s1, 3, d1, d2 );
		
//		public StopTime(int tripId, Trip trip, int stopId, Stop stop, int stopSequence, String arrivalTime, String departureTime)
		
		rt1.bestEnd = s1;
		rt1.bestStart=s3;
		rt1.routeEnd=r1;
		rt1.routeStart=r1;
		rt1.endTime= d2;
		rt1.startTime=d1;
		//FEMMAN
		
		
		
		rt2.bestEnd = s1;
		rt2.bestStart=s3;
		rt2.routeEnd=r3;
		rt2.routeStart=r2;
		rt2.endTime= d2;
		rt2.startTime=d1;
		rt2.transferEndTime=st1.arrivalTime();
		rt2.transferStartTime=st2.departureTime();
		
		rt2.transfer=true;
		//ETTAN-FYRAN
		
		rt3.bestEnd = s1;
		rt3.bestStart=s3;
		rt3.routeEnd=r4;
		rt3.routeStart=r2;
		rt3.endTime= d2;
		rt3.startTime=d1;
		rt3.transferEndTime=st1.arrivalTime();
		rt3.transferStartTime=st2.departureTime();
		rt3.transfer=true;
		//ETTAN-ÅTTAN
		
		
		recTrips.add(rt1);
		recTrips.add(rt2);
		recTrips.add(rt3);
		
		//Add a start and a stop and times and name
		
		comeve = new CommuteEvent();
		
		
		//END HARDCODED DEMO DATA
		
		
		
		
		
		
			
		
		
		String filePath = "M7012E.ics";
		AssetManager assetManager = getAssets();
		String data = null;
		
		//android.util.Log.println(1, "TEST0", assetManager.toString());
		
		try {
			InputStream instream = assetManager.open(filePath); 
			
			System.out.println("TEST try" + instream.toString());
			android.util.Log.println(2, "TEST try", instream.toString());
			data = Parser.readFileAsString(instream);
			
		} catch (Exception e) {

			System.out.println("TEST catch" + e.toString());
			android.util.Log.println(2, "TEST" , e.getMessage().toString());
			e.getMessage();
		}
		
		complan = new CommutePlanner(500, 15);	
		System.out.println("TEST" + data);
		android.util.Log.println(2, "TEST", data);
		
		comeve = complan.nextEvent(data, lat,lon);
		comeve.setTrips(recTrips);
		
		
		//CommuteEvent comEvent = new CommuteEvent();
		
	}
	
	public void toViewEvent(View v) {
		Intent intent = new Intent(getApplicationContext(), ListActivity.class);
				
        //Get Upcoming Event
		
        intent.putExtra("EXTRA_EVENT_TITLE", comeve.getEventActivity().description());
        
        SimpleDateFormat format = new SimpleDateFormat("MMM dd HH:mm");
        String start_time = format.format(comeve.getEventActivity().start());
        String end_time = format.format(comeve.getEventActivity().end());
        
		intent.putExtra("EXTRA_EVENT_START", start_time + "");
		intent.putExtra("EXTRA_EVENT_END", end_time);
		intent.putExtra("EXTRA_EVENT_LOCATION", "Luleå Tekniska Universitet");
		
		startActivity(intent);
		
    }
	public void toViewHome(View v) {
		Intent intent = new Intent(getApplicationContext(), ListActivity.class);
		
		Calendar c = Calendar.getInstance(); 
		Integer hour = c.get(Calendar.HOUR_OF_DAY);
		Integer minute = c.get(Calendar.MINUTE);
		String time = "[placeholder]";
		
		if (minute < 10) {
			time  = hour.toString() + ":" + "0" + minute.toString();
		} else {
			time = hour.toString() + ":" + minute.toString();
		}
		
		intent.putExtra("EXTRA_EVENT_TITLE", "Go Home");
		intent.putExtra("EXTRA_EVENT_START", time);
		intent.putExtra("EXTRA_EVENT_END", "[placeholder]");
		intent.putExtra("EXTRA_EVENT_LOCATION", "getYourPredefinedAddress()");
		
		startActivity(intent);

		
    }
	public void toViewSettings(View v) {
		Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
		

		//GET CURRENT GPS LOCATION
		LocationManager clocManager = null;  
        LocationListener clocListener;  
        clocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        clocListener = new CommuteLocationListener();  
        clocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, clocListener);
        
        lon = 0.0;
		lat = 0.0; 
		
        String status = "GPS is OFF";
        
        if (clocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  
        	
        	if(CommuteLocationListener.latitude > 0){  
                 lat = CommuteLocationListener.latitude;  
                 lon = CommuteLocationListener.longitude;  
                 status = "GPS is ON";
        	} else {  
            	 status = "GPS is confused.";
            }  
        } else {  
              status = "GPS Is OFF.";  
        } 
        
        intent.putExtra("EXTRA_GPS_LON", lon);
        intent.putExtra("EXTRA_GPS_LAT", lat);
        intent.putExtra("EXTRA_GPS_STATUS", status);
        
        
        
		startActivity(intent);

		
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
